package com.linkin.homepage.base.recycleview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.linkin.homepage.base.viewmapping.ViewMapUtil;

public abstract class BaseMultiRCAdapter extends BaseRCAdapter<BaseRCViewHolder> {

    private Class[] mClazzList = new Class[32];
    private Class[] mCachedClazzList = new Class[32];
    private int[] mCachedClazzTypeList = new int[32];
    private int mCachedClazzCount = 0;

    protected abstract Class getItemHolderClz(Object item, int position);

    public BaseMultiRCAdapter(Context context) {
        super(context);
    }

    @Override
    public int getItemViewType(int position) {
        Class clazz = getItemHolderClz(getItem(position), position);
        if (clazz != null) {
            for (int i = 0; i < mCachedClazzCount; i++) {
                if (mCachedClazzList[i] == clazz) {
                    return mCachedClazzTypeList[i];
                }
            }
            int type = getItemTypeByViewHolderClass(clazz);
            if (type != 0) {
                mClazzList[type - 1] = clazz;
                mCachedClazzList[mCachedClazzCount] = clazz;
                mCachedClazzTypeList[mCachedClazzCount] = type;
                mCachedClazzCount++;
            }
            return type;
        }
        return 0;
    }

    private int getItemTypeByViewHolderClass(Class<?> clazz) {
        ViewType viewType = clazz.getAnnotation(ViewType.class);
        if (viewType != null) {
            return viewType.value();
        }

        int type = 0;
        int base = 1;
        String className = clazz.getSimpleName();
        int length = className.length();
        for (int i = length - 1; i >= 0; i--) {
            char character = className.charAt(i);
            if (character >= '0' && character <= '9') {
                type += (character - '0') * base;
                base *= 10;
                continue;
            }
            break;
        }
        return type;
    }

    @Override
    public BaseRCViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mViewParent == null) {
            mViewParent = parent;
        }
        if (viewType > 0) {
            Class<?> clazz = (Class<?>) mClazzList[viewType - 1];
            return (BaseRCViewHolder) ViewMapUtil.mapForRecyclerView(mContext, clazz, parent);
        }
        return null;
    }

    protected Class<BaseRCViewHolder> getHolderClz() {
        return null;
    }

    @Override
    protected void onBindView(BaseRCViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 1:
                onBindView1(holder, position);
                break;
            case 2:
                onBindView2(holder, position);
                break;
            case 3:
                onBindView3(holder, position);
                break;
            case 4:
                onBindView4(holder, position);
                break;
            case 5:
                onBindView5(holder, position);
                break;
            case 6:
                onBindView6(holder, position);
                break;
            case 7:
                onBindView7(holder, position);
                break;
            case 8:
                onBindView8(holder, position);
                break;
        }
    }

    @Override
    protected void onClickView(BaseRCViewHolder holder, int position, View v) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 1:
                onClickView1(holder, position, v);
                break;
            case 2:
                onClickView2(holder, position, v);
                break;
            case 3:
                onClickView3(holder, position, v);
                break;
            case 4:
                onClickView4(holder, position, v);
                break;
            case 5:
                onClickView5(holder, position, v);
                break;
            case 6:
                onClickView6(holder, position, v);
                break;
            case 7:
                onClickView7(holder, position, v);
                break;
            case 8:
                onClickView8(holder, position, v);
                break;
        }
    }

    protected void onBindView1(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView2(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView3(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView4(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView5(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView6(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView7(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView8(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView9(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView10(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView11(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView12(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView13(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView14(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView15(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView16(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView17(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView18(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView19(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView20(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView21(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView22(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView23(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView24(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView25(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView26(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView27(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView28(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView29(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView30(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView31(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onBindView32(BaseRCViewHolder viewHolder, int position) {
    }

    protected void onClickView1(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView2(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView3(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView4(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView5(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView6(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView7(BaseRCViewHolder viewHolder, int position, View v) {
    }

    protected void onClickView8(BaseRCViewHolder viewHolder, int position, View v) {
    }

}
